package ihooyah.com.whitehat.utils

import android.app.Activity
import android.content.DialogInterface
import android.graphics.Color
import android.text.InputType
import android.text.TextUtils
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import ihooyah.com.whitehat.R
import org.jetbrains.anko.*

/**
 * Created by debby on 2018/12/29.
 * 描述:各种对话框
 */

//接口列表

class OnButtonClickListener(confirms: () -> Unit) {
    var confirm: () -> Unit = confirms
}

fun showSimpleAlert(activity: Activity, message: String, confirm: () -> Unit) {
    activity.alert(message) {
        yesButton {
            confirm()
        }
        noButton { }
    }.show()
}


//控件列表对话框
fun showButtonList(activity: Activity, pairList: ArrayList<Pair<String, OnButtonClickListener>>) {
    var dialog: DialogInterface? = null

    dialog = activity.alert {
        title = "功能选择"
        customView {
            linearLayout {
                orientation = LinearLayout.VERTICAL
                padding = dip(16)

                for ((count, pair) in pairList.withIndex()) {
                    button {
                        text = pair.first
                        setOnClickListener {
                            pair.second.confirm()
                            dialog!!.dismiss()
                        }
                        //样式
                        background = getRoundShape(
                            8f,
                            ContextCompat.getColor(activity, R.color.blue)
                        )
                        textColor = Color.WHITE
                    }.lparams(width = LinearLayout.LayoutParams.MATCH_PARENT) {
                        topMargin = 10
                        bottomMargin = 10
                        if (count == pairList.size) {
                            bottomMargin = 40
                        }
                    }
                }
            }
        }
    }.show()
}


//修改密码
fun alertChangePassword(activity: Activity, setPassword: (String) -> Unit) {
    var dialog: DialogInterface? = null
    dialog = activity.alert("是否修改密码") {
        customView {
            linearLayout {
                orientation = LinearLayout.VERTICAL
                val e = editText {
                    setPadding(40, 20, 0, 20)
                    hint = "输入新密码"
                    textSize = 18f
                    background = getRoundCornerShape(
                        8f,
                        ContextCompat.getColor(activity, R.color.blue)
                    )
                }.lparams(width = LinearLayout.LayoutParams.MATCH_PARENT) {
                    bottomMargin = 40
                }

                button("修改") {
                    textColor = Color.WHITE
                    background = getRoundShape(
                        8f,
                        ContextCompat.getColor(activity, R.color.blue)
                    )

                    this.setOnClickListener {
                        val password = e.text.toString()
                        setPassword(password)
                        dialog!!.dismiss()
                    }
                }
                padding = dip(16)
            }
        }
    }.show()
}

//延时息屏对话框
fun alertInput(activity: Activity, onSure: (Long) -> Unit) {
    var dialog: DialogInterface? = null

    dialog = activity.alert {
        title = "延时息屏"
        customView {
            linearLayout {
                orientation = LinearLayout.VERTICAL
                padding = dip(16)

                val e = editText {
                    hint = "设置定时息屏时间(秒)"
                    inputType = InputType.TYPE_CLASS_NUMBER
                }
                button("确定") {
                    background = getRoundShape(
                        8f,
                        ContextCompat.getColor(activity, R.color.orange)
                    )
                    textColor = Color.WHITE
                    this.setOnClickListener {
                        var time = e.text.toString()
                        if (TextUtils.isEmpty(time)) {
                            time = "30"
                        }
                        onSure(time.toLong() * 1000)
                        dialog!!.dismiss()
                    }
                }
            }
        }
    }.show()
}

//是否禁用摄像头
fun showCameraDisabled(activity: Activity, isDisable: Boolean, onSwitch: (Boolean) -> Unit) {
    var dialog: DialogInterface? = null
    dialog = activity.alert {
        title = "禁用摄像头？"
        customView {
            linearLayout {
                orientation = LinearLayout.VERTICAL
                padding = dip(16)
                switch {
                    text = "禁用"
                    isChecked = isDisable
                    setOnCheckedChangeListener { _, isChecked ->
                        onSwitch(isChecked)
                        dialog!!.dismiss()
                    }
                }
            }
        }
    }.show()
}




